<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Gesture Recognizers  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
  </head>
  <body>
    <a name="//apple_ref/swift/Section/Gesture Recognizers" class="dashAnchor"></a>
    <a title="Gesture Recognizers  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">Closures Docs</a></p>
        <p class="header-right"><a href="https://github.com/vhesener/Closures"><img src="img/gh.png"/>View on GitHub</a></p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">Closures Reference</a>
        <img id="carat" src="img/carat.png" />
        Gesture Recognizers  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Controls.html">Controls</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIButton.html">UIButton</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISwitch.html">UISwitch</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UITextField.html">UITextField</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIDatePicker.html">UIDatePicker</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPageControl.html">UIPageControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISegmentedControl.html">UISegmentedControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIStepper.html">UIStepper</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISlider.html">UISlider</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIControl.html">UIControl</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIBarButtonItem.html">UIBarButtonItem</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Scrolling Views.html">Scrolling Views</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UITableView.html">UITableView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UICollectionView.html">UICollectionView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPickerView.html">UIPickerView</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIScrollView.html">UIScrollView</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Gesture Recognizers.html">Gesture Recognizers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIGestureRecognizer.html">UIGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UITapGestureRecognizer.html">UITapGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UILongPressGestureRecognizer.html">UILongPressGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPinchGestureRecognizer.html">UIPinchGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIPanGestureRecognizer.html">UIPanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UISwipeGestureRecognizer.html">UISwipeGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIRotationGestureRecognizer.html">UIRotationGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIScreenEdgePanGestureRecognizer.html">UIScreenEdgePanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIView.html">UIView</a>
              </li>
              <li class="nav-group-task">
                <a href="Gesture Recognizers.html#/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF">configure(gesture:handler:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Controllers.html">Controllers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController.html">UIImagePickerController</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController/MediaFilter.html">– MediaFilter</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UIImagePickerController/Result.html">– Result</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="KVO.html">KVO</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/_5FKeyValueCodingAndObserving.html">_KeyValueCodingAndObserving</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>Gesture Recognizers</h1>
            <p>The <code>UIGestureRecognizer</code> initializers and delegation wrappers
 make it easy to add gesture recognizers to views. It also uses
 closures instead of target-action and delegation.</p>
<h2 id='target-action-initializers' class='heading'>Target-Action Initializers</h2>

<p>The following is how you would add a double tap gesture
 recognizer to your view using one of the custom initializers.
 As always, we have a closure handler to respond to the gesture&rsquo;s
 double tap action.</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">doubleTapGesture</span> <span class="o">=</span> <span class="kt">UITapGestureRecognizer</span><span class="p">(</span><span class="nv">tapsRequired</span><span class="p">:</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span> <span class="n">_</span> <span class="k">in</span>
    <span class="nf">log</span><span class="p">(</span><span class="s">"double tapped"</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">view</span><span class="o">.</span><span class="nf">addGestureRecognizer</span><span class="p">(</span><span class="n">doubleTapGesture</span><span class="p">)</span>
</code></pre>

<p>These convenience initializers, delegate closures, and closure recognizers
have been added to all of the existing concrete subclasses, including:</p>

<ul>
<li><code>UITapGestureRecognizer</code></li>
<li><code>UIPinchGestureRecognizer</code></li>
<li><code>UIRotationGestureRecognizer</code></li>
<li><code>UISwipeGestureRecognizer</code></li>
<li><code>UIPanGestureRecognizer</code></li>
<li><code>UIScreenEdgePanGestureRecognizer</code></li>
<li><code>UILongPressGestureRecognizer</code></li>
</ul>

<p>There is also a method for you to configure a custom gesture recognizer
to use closure handlers for recognition:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">myCustomGesture</span> <span class="o">=</span> <span class="kt">MyCustomGestureRecognizer</span><span class="p">()</span>
<span class="nf">configure</span><span class="p">(</span><span class="nv">gesture</span><span class="p">:</span> <span class="n">myCustomGesture</span><span class="p">)</span> <span class="p">{</span> <span class="n">_</span> <span class="k">in</span>
    <span class="c1">/// a closure that's called when the gesture has ended</span>
<span class="p">}</span>
</code></pre>
<h2 id='delegation' class='heading'>Delegation</h2>

<p>With convenient extension methods on <code>UIGestureRecognizer</code> and <code>UIView</code>,
we can daisy chain an entire gesture cycle, including responding
to <code>UIGestureRecognizerDelegate</code> methods.</p>
<pre class="highlight swift"><code><span class="n">view</span>
    <span class="o">.</span><span class="nf">addPanGesture</span><span class="p">()</span> <span class="p">{</span> <span class="n">pan</span> <span class="k">in</span>
        <span class="k">guard</span> <span class="n">pan</span><span class="o">.</span><span class="n">state</span> <span class="o">==</span> <span class="o">.</span><span class="n">ended</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="p">}</span>
        <span class="nf">log</span><span class="p">(</span><span class="s">"view panned"</span><span class="p">)</span>
    <span class="p">}</span><span class="o">.</span><span class="nf">shouldBegin</span><span class="p">()</span> <span class="p">{</span>
        <span class="kc">true</span>
    <span class="p">}</span><span class="o">.</span><span class="n">shouldRecognizeSimultaneouslyWith</span> <span class="p">{</span>
        <span class="nv">$0</span> <span class="o">===</span> <span class="n">doubleTapGesture</span>
<span class="p">}</span>
</code></pre>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UIGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIGestureRecognizer">UIGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UITapGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UITapGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UITapGestureRecognizer">UITapGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UITapGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UITapGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UILongPressGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UILongPressGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UILongPressGestureRecognizer">UILongPressGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UILongPressGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UILongPressGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIPinchGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UIPinchGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIPinchGestureRecognizer">UIPinchGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIPinchGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIPinchGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIPanGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UIPanGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIPanGestureRecognizer">UIPanGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIPanGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIPanGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UISwipeGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UISwipeGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UISwipeGestureRecognizer">UISwipeGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UISwipeGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UISwipeGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIRotationGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UIRotationGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIRotationGestureRecognizer">UIRotationGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIRotationGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIRotationGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIScreenEdgePanGestureRecognizer"></a>
                    <a name="//apple_ref/swift/Extension/UIScreenEdgePanGestureRecognizer" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIScreenEdgePanGestureRecognizer">UIScreenEdgePanGestureRecognizer</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIScreenEdgePanGestureRecognizer.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIScreenEdgePanGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/c:objc(cs)UIView"></a>
                    <a name="//apple_ref/swift/Extension/UIView" class="dashAnchor"></a>
                    <a class="token" href="#/c:objc(cs)UIView">UIView</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        
                        <a href="Extensions/UIView.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">UIView</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF"></a>
                    <a name="//apple_ref/swift/Function/configure(gesture:handler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF">configure(gesture:handler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>This method is a convenience method to add a closure handler to a custom subclass of
UIGestureRecognizer. If creating a custom gesture recognizer, you may want to also
provide an initializer that takes a completion handler, just as the Closures
framework provides for UIKit gesture recognizers.</p>

<hr>
<h4 id='an-example-of-an-initializer-that-adds-closure-support-for-recognition-of-custom-gesture-recognizer' class='heading'>An example of an initializer that adds closure support for recognition of custom gesture recognizer:</h4>
<pre class="highlight swift"><code><span class="kd">class</span> <span class="kt">MyCustomGestureRecognizer</span><span class="p">:</span> <span class="kt">UIGestureRecognizer</span> <span class="p">{</span>
    <span class="kd">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="n">_</span> <span class="nv">gesture</span><span class="p">:</span> <span class="kt">MyCustomGestureRecognizer</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>
        <span class="nf">configure</span><span class="p">(</span><span class="nv">target</span><span class="p">:</span> <span class="k">self</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="n">handler</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="n">configure</span><span class="o">&lt;</span><span class="kt">T</span><span class="o">&gt;</span><span class="p">(</span><span class="nv">gesture</span><span class="p">:</span> <span class="kt">T</span><span class="p">,</span> <span class="nv">handler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="n">_</span> <span class="nv">gesture</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span>
    <span class="k">where</span> <span class="kt">T</span><span class="p">:</span> <span class="kt">UIGestureRecognizer</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>gesture</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The UIGestureRecognizer that is being configured to use a closure in place of target-action.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>handler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure that will be called when the gesture is recognized.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>2017 Vincent Hesener</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.10.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
